package drive.RemakeController;

import drive.Model.Article;
import drive.Model.User;
import drive.RemakeService.ServiceInterface.UserProfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user-profile")
public class UserProfileController {

    @Autowired
    private UserProfileService userProfileService;
    //注册
    @PostMapping("/register")
    public String registerUser(@RequestBody User user) {
        return userProfileService.registerUser(user);
    }
    //登录
    @PostMapping("/login")
    public String loginUser(@RequestBody User user) {
        String username = user.getUsername();
        String password = user.getPassword();
        return userProfileService.loginUser(username, password);
    }

    @PostMapping("/login_admin")
    public String loginUserAdmin(@RequestBody User user) {
        String username = user.getUsername();
        String password = user.getPassword();
        return userProfileService.loginUserAdmin(username, password);
    }
    //更新用户信息
    @PostMapping("/update-profile")
    public String updateUserProfile(@RequestBody User user) {
        return userProfileService.updateUserProfile(user);
    }
    //根据用户ID查看收藏
    @GetMapping("/view-collections")
    public String viewUserCollections(Integer userID,int PageNum,int PageSize) {
        return userProfileService.viewUserCollections(userID,PageNum,PageSize);
    }
    //根据用户ID查询
    @GetMapping("/view-messages")
    public String viewUserMessages(Integer userID) {
        return userProfileService.viewUserMessages(userID);
    }
    //关注用户
    @PostMapping("/follow")
    public String followUser(@RequestHeader("token") String token,Integer targetUserID) {
        return userProfileService.followUser(token, targetUserID);
    }
    //根据用户ID查询文章
    @GetMapping("/view-published-articles")
    public String viewUserPublishedArticles(Integer userID,int PageNum,int PageSize) {
        return userProfileService.viewUserPublishedArticles(userID,PageNum,PageSize);
    }
    //修改文章
    @PostMapping("/modify-article")
    public String modifyUserPublishedArticle(@RequestBody Article article) {
        return userProfileService.modifyUserPublishedArticle(article);
    }
    //获取有多少人关注自己
    @GetMapping("/follow/count")
    public String getFollowCount(@RequestParam int userId) {
        return userProfileService.getFollowCount(userId);
    }
    //获取自己关注了多少人
    @GetMapping("/following/count")
    public String getFollowingCount(@RequestParam int userId) {
        return userProfileService.getFollowingCount(userId);
    }
    //根据用户ID获取个人信息
    @GetMapping("/info")
    public String getUserInfo(@RequestParam int userId) {
        return userProfileService.getUserInfo(userId);
    }
    //取消关注
    @DeleteMapping("/unfollow")
    public String unfollow(@RequestParam int followerId, @RequestParam int followedId) {
        return userProfileService.unfollow(followerId, followedId);
    }

    //根据用户ID获取已发布文章数量
    @GetMapping("/article/count")
    public String getUserArticleCount(@RequestParam int userId) {
        return userProfileService.getUserArticleCount(userId);
    }

    /**
     * 根据文章ID获取文章的收藏数量和评论数量
     * @param articleId 文章ID
     * @return 包含文章ID、收藏数量和评论数量的JSON字符串
     */
    @GetMapping("/stats")
    public String getArticleStats(@RequestParam int articleId) {
        return userProfileService.getArticleStats(articleId);
    }

    /**
     * 根据用户ID查询聊天室
     * @param userId 用户ID
     * @return 包含聊天室信息的JSON字符串
     */
    @GetMapping("/chatroom")
    public String getUserChatRooms(@RequestParam int userId) {
        return userProfileService.getUserChatRooms(userId);
    }

    /**
     * 创建聊天室
     * @param user1ID 用户1的ID
     * @param user2ID 用户2的ID
     * @return 包含聊天室信息的JSON字符串
     */
    @PostMapping("/create")
    public String createChatRoom(@RequestParam int user1ID, @RequestParam int user2ID) {
        return userProfileService.createChatRoom(user1ID, user2ID);
    }

    /**
     * 查询聊天室的第一条消息
     * @param chatRoomId 聊天室ID
     * @return 包含第一条消息信息的JSON字符串
     */
    @GetMapping("/firstmessage")
    public String getFirstMessage(@RequestParam int chatRoomId) {
        return userProfileService.getFirstMessage(chatRoomId);
    }

    /**
     * 发送消息到指定的聊天室
     * @param chatRoomId 聊天室ID
     * @param senderId 发送者ID
     * @param content 消息内容
     * @return 操作结果的JSON字符串
     */
    @PostMapping("/send")
    public String sendMessage(@RequestParam int chatRoomId, @RequestParam int senderId, @RequestParam String content) {
        return userProfileService.sendMessage(chatRoomId, senderId, content);
    }

    /**
     * 分页获取聊天室内的消息
     * @param chatRoomId 聊天室ID
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 包含消息列表和分页信息的JSON字符串
     */
    @GetMapping("/messages")
    public String getChatRoomMessages(
            @RequestParam int chatRoomId,
            @RequestParam int pageNum,
            @RequestParam int pageSize) {
        return userProfileService.getChatRoomMessages(chatRoomId, pageNum, pageSize);
    }

    /**
     * 用户点开聊天室后将当前所有消息标记为已读
     * @param chatRoomId 聊天室ID
     * @param userId 用户ID
     * @return 操作结果的JSON字符串
     */
    @PostMapping("/markasread")
    public String markAllMessagesAsRead(@RequestParam int chatRoomId, @RequestParam int userId) {
        return userProfileService.markAllMessagesAsRead(chatRoomId, userId);
    }

    /**
     * 根据用户ID获取二手物品
     * @param userId 用户ID
     * @return 包含二手物品信息的JSON字符串
     */
    @GetMapping("/secondhanditems")
    public String getUserSecondHandItems(@RequestParam int userId) {
        return userProfileService.getUserSecondHandItems(userId);
    }

    /**
     * 根据用户ID查询灾害
     * @param userId 用户ID
     * @return 包含灾害信息的JSON字符串
     */
    @GetMapping("/disasters")
    public String getUserDisasters(@RequestParam int userId) {
        return userProfileService.getUserDisasters(userId);
    }

    @GetMapping("/view-itemcollections")
    public String viewItemCollections(Integer userID,int PageNum,int PageSize) {
        return userProfileService.viewItemCollections(userID,PageNum,PageSize);
    }

    /**
     * 分页获取用户关注的人的文章列表
     * @param userId 用户ID
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 包含文章列表和分页信息的JSON字符串
     */
    @GetMapping("/followed-articles")
    public String getFollowedUsersArticles(
            @RequestParam int userId,
            @RequestParam int pageNum,
            @RequestParam int pageSize) {
        return userProfileService.getFollowedUsersArticles(userId, pageNum, pageSize);
    }

    /**
     * 获取用户关注的用户信息
     * @param userId 用户ID
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 包含关注用户信息和分页信息的JSON字符串
     */
    @GetMapping("/followed-users")
    public String getFollowedUsers(
            @RequestParam int userId,
            @RequestParam int pageNum,
            @RequestParam int pageSize) {
        return userProfileService.getFollowedUsers(userId, pageNum, pageSize);
    }

    /**
     * 根据用户ID获取关注列表
     * @param userId 用户ID
     * @return 包含关注列表的JSON字符串
     */
    @GetMapping("/follows_list")
    public String getFollows(@RequestParam int userId) {
        return userProfileService.getFollowslist(userId);
    }
}